<?php
session_start();
require_once( 'employee_db.php' );
 
$type = new M_Employee();
$user_id = $_SESSION['userid'];

if (isset($_POST['m'])  &&  $_POST['m']!=""){

    if ($_POST['m']== 'load'){
      $code = $_POST['code'];
	  $name = $_POST['name'];
	  $status = $_POST['status'];
	  $depart = $_POST['depart'];
	  $idcard = $_POST['idcard'];
	  
      $result=$type->loadData($code,$name,$depart,$status,"",$idcard);
      $rows=$type->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id']
								,$row['id_card']
								,$row['code']
								,$row['title'] .' '. $row['name']
								,$row['departmentname']
								,$row['telephone']
								,$row['statusname']
								, $row['address']
								,$row['education']
								, $row['did']
								, $row['status']
								, $row['title_id'] 
								,$row['datein'] 
								, $row['name']
								, $row['salary']
								, $row['account']
								, $row['course_fee']
								) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
      
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['date_in']="";
		if ($_POST['date_in'] !=''){
			list($d,$m,$y) = split('/', $_POST['date_in']);
			$ar['date_in']= $y.'/'.$m.'/'.$d;
		}
		$ar['code']=  $_POST['code'];
		$ar['name']=  $_POST['name'];
		$ar['title']=  $_POST['title'];
		$ar['id_card'] = $_POST['id_card'];
		$ar['status']=  $_POST['status'];
		$ar['address']=  $_POST['address'];
		$ar['account']=  $_POST['account'];
		$ar['telephone']=  $_POST['tele'];
		$ar['did']=  $_POST['department'];
		$ar['salary']=  $_POST['salary'];
		$ar['education'] = $_POST['education'];
		$ar['course_fee']=$_POST['course_fee'];
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		
		$return_id = $type->checkCode($ar['code']);
		if ($return_id ==0){
			$id=$type->InsertData($ar);
			$type->AddSalaryById($id,$user_id);
			echo 1;
		}else{
			echo 0;
		}
		
    }else if ($_POST['m']=='edit'){
		$ar = array();
		$ar['date_in']="";
		
		if ($_POST['date_in'] !=''){
			list($d,$m,$y) = split('/', $_POST['date_in']);
			$ar['date_in']= $y.'/'.$m.'/'.$d;
		}
		$ar['code']=  $_POST['code'];
		$ar['name']=  $_POST['name'];
		$ar['title']=  $_POST['title'];
		$ar['id_card'] = $_POST['id_card'];
		$ar['status']=  $_POST['status'];
		$ar['address']=  $_POST['address'];
		$ar['telephone']=  $_POST['tele'];
		$ar['account']=  $_POST['account'];
		$ar['salary']=  $_POST['salary'];
		$ar['course_fee']=$_POST['course_fee'];
		$ar['did']=  $_POST['department'];
		$ar['education'] = $_POST['education'];
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] = $user_id;
		 
		$strcipt = $type->updateData($_POST['id'],$ar);
		$type->updateSalary($_POST['id'], $_POST['salary'] , $user_id);
		// echo json_encode($strcipt);
	}
	else if ($_POST['m']=='del'){
         $id=$_POST['id'];
		 $type->deleData($id);
		 
    }/// Age Criteria
	    
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if($_GET['m']=='gy')
			echo json_encode( $type->getUserStatus());
		else if($_GET['m']=='g')
			echo json_encode( $type->getUserStatus('Y'));
		else if ($_GET['m']=='d')
			echo json_encode( $type->getDepartment());
		else if ($_GET['m']=='dy')
			echo json_encode( $type->getDepartment('Y'));
	}
}
 
?>